package com.study.controller;

//import com.alibaba.fastjson.JSON;
//import com.github.pagehelper.PageHelper;
//import com.github.pagehelper.PageInfo;
//import com.study.domain.Book;
//import com.study.mapper.BookMapper;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.domain.Book;
import com.study.mapper.BookMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
//import java.util.logging.Logger;

@RestController
public class BookController {

    Logger logger = LoggerFactory.getLogger(BookController.class);

    @Autowired
    BookMapper bookMapper;

    //@RequestMapping("selectAll")
    //public Object selectAll() {
    //    List<Book> books = bookMapper.selectAll();
    //    return books;
    //}

//    @RequestMapping("/selectAllPage")
//    public Object selectAllPage(HttpServletRequest request) {
//        System.out.println(request.getParameter("currentPage"));//查看一下后台能否获取到前端传过来的页码
//        int currentPage = Integer.parseInt(request.getParameter("currentPage"));//把String转化为int
////        //获取第currentPage页，10条内容，默认查询总数count
//        PageHelper.startPage(currentPage, 10);
//        List<Book> books = bookMapper.selectAll();
//        //用PageInfo对结果进行包装
//        PageInfo page = new PageInfo(books);
//        return page;
//    }

    @RequestMapping("/selectAllPageLike")
    public Object selectAllPageLike(HttpServletRequest request) {
//        System.out.println("currentPage="+request.getParameter("currentPage"));//查看一下后台能否获取到前端传过来的页码
//        System.out.println("type="+request.getParameter("type"));//查看一下后台能否获取到条件查询传过来的type
//        System.out.println("name="+request.getParameter("name"));//查看一下后台能否获取到条件查询传过来的name
//        System.out.println("description="+request.getParameter("description"));//查看一下后台能否获取到条件查询传过来的description
        int currentPage = Integer.parseInt(request.getParameter("currentPage"));//把String转化为int

        Book book = new Book(request.getParameter("type"), request.getParameter("name"), request.getParameter("description"));//封装条件查询传过来的3个条件

//        //获取第currentPage页，10条内容，默认查询总数count
        PageHelper.startPage(currentPage, 10);
//        List<Book> books = bookMapper.selectAllLike(String type,String name,String description);
        List<Book> books = bookMapper.selectAllLike(book);
        //用PageInfo对结果进行包装
        PageInfo page = new PageInfo(books);
//        if (page.getSize()<=10){
//            page.setPageNum(1);
//        }
        return page;
    }


    @RequestMapping("insertForm")//新增
    public void insertForm(HttpServletRequest request) throws IOException {

        //1 得到前端发送过来的form表单数据

        //request.getParameter("type");//这样只能得到null

        //思考前端发送过来的数据是什么格式？？
        //知道是json格式了,我们可以通过IO流的方式来读取
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        System.out.println("s=" + s);//s={"type":"11","name":"22","description":"33"}

        //需要把json格式数据转化为我们好理解的domain实体类格式
        Book book = JSON.parseObject(s, Book.class);
        System.out.println("book=" + book);


        int i = bookMapper.insertData(book);
        System.out.println("插入数据条数=" + i);


    }


    @RequestMapping("deleteLine")
    public void deleteLine(HttpServletRequest request) throws IOException {

        System.out.println("进入删除RequestMapping deleteLine");
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        System.out.println("s=" + s);
        Book book = JSON.parseObject(s, Book.class);
        System.out.println("book=" + book);

        book.getId();//这里得到的id直接就是数据库的主键id

        int i = bookMapper.deleteData(book.getId());
        System.out.println("删除数据条数=" + i);
    }

    @RequestMapping("updateForm")//修改
    public void updateForm(HttpServletRequest request) throws IOException {
        //1 得到前端发送过来的form表单数据
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        System.out.println("s=" + s);//s={"id":10,"type":"1","name":"2","description":"3"}

        //需要把json格式数据转化为我们好理解的domain实体类格式
        Book book = JSON.parseObject(s, Book.class);
        System.out.println("book=" + book);

        int i = bookMapper.updateData(book);

        if (i > 0) {
            System.out.println("成功了");
            System.out.println("修改数据条数=" + i);
        } else {
            System.out.println("失败了");

        }

    }


}
